  function [y,yname,cvec,trainvec,meany,stdy]=load_dset(dfname,dpath,into,demean_type,onscreen)
% function
% [y,yname,cvec]=load_dset(dfname,dpath,into,demean_type,onscreen_); 
%
%
% Load dataset in the Excel file dfname (no need to provide excel extension) 
% Stored in path dpath 
%
% First Observation should be the calendar vector (numeric) 
% File should not have any missing values 
%
% Structure into should contain fields (default values ) 
% -------------------------------------------------------
% into.start        Date Begin sample ( default is cvec(1) ) 
% into.finish       Date End   sample ( cvec(end) ) 
% into.startest     Date Begin Estimation ( into.start ) 
% into.finishest    Date End   Estimation ( into.finish ) 
% into.demean       Demean data? ==1 Yes, ==0 No 
% DEMEAN_TYPE       == 1 Whole sample (training + est ) mean 
%                   == 2 Estimation sample only 
%                   Default is == 1 
% ONSCREEN         if ==1, not provided or empty, will display onscreen 
% Output 
% ------
% y         data [ T x n ]
% yname     cell with series name 
% cvec      calendar vector 
% trainvec  location of the first and last observation for the computation
%           of the likelihood 
% meany     mean y 
% stdy      standard deviation y 
% 
% Alejandro Justiniano   2/10/2005
%                       11/22/2005
% 6/29/2010. Removed error if NANS 
%            In the presence of NANS check which observations have 
%            NANS, which do not an number of observations 
% =========================================================================
dispaj('Data file: ',dfname); 
pause(0.5); 
dispaj('Location: ',dpath); 

cucd=cd; 
if nargin < 5 | isempty( onscreen); 
    onscreen=1; 
end 
if nargin < 4 | isempty( demean_type ); 
    demean_type = 1; 
else 
    if demean_type~=1 & demean_type~=2; 
        error('DEMEAN_TYPE must be 1 (default) or 2') 
    end 
end 
if nargin < 3; error('Missing one input'); end; 
cd(dpath); 
eval(dfname); 
cd(cucd);

datm = Y; 
sernam = Ynames; 

cvec=datm(:,1);
y=datm(:,2:end);
% ============================
% Check for non missing values 
% Modifiefd June 2010 
% ============================
[T,n]=size(y); 
ii=find( isnan(y) ~= 0);
if sum(ii) ~= 0
    flag_datanan=1;
    sernam=sernam(1,:);
    disp('_______________________________________________');
    disp('Dataset contains NANS');
    disp('Full Sample settings' ); 
    [meanyf,stdyf,flag_nan,numnan]=moments_nan(y,1,sernam(2:end));
    if onscreen==1
        quer('c')
    else
        pause(5)
        disp('Continue with estimation')
    end
else
    disp('Full Sample settings' );
    [meanyf,stdyf,flag_nan,numnan]=moments_nan(y,1,sernam(2:end));    
end
% Check series name 
[nc1,nc2]=size(sernam);  
if nc1 > 1 | nc2 ~= (n+1) 
    error('Check the data strings in the dataset')  
end 
yname=sernam(2:end); 
% =========================================================================
%           Check the settings in into are correct 
% =========================================================================
[junk,into]=ch_field( into, 'start' , cvec(1) ); 
staf=find(  (cvec == into.start)  );
if isempty( staf ) ==1   | length( staf ) > 1 
    error(['Starting data ',num2str( into.start ),' is outside sample range'] )
end 
[junk,into]=ch_field( into, 'finish' , cvec(end) ); 
endf=find( (cvec == into.finish)  );
if isempty( endf ) |  length( endf ) > 1 
    error(['Ending data ',num2str( into.finish ),' is outside sample range'] )
end 
[junk,into]=ch_field( into, 'startest' , into.start ); 
estb = find(  (cvec  ==  into.startest)  );
if isempty( estb )  | length( estb ) > 1 
    error(['Starting date for estimation  ',num2str( into.startest ),' is outside sample range'] )
end 
[junk,into]=ch_field( into, 'finishest' , into.finish ); 
este = find(  (cvec == into.finishest)  );
if isempty( este )  | length( este ) > 1 
    error(['Finishing date for estimation  ',num2str( into.startest ),' is outside sample range'] )
end 
temp_1 =[ este endf  estb staf]; 
temp_2 =[ endf estb staf     1]; 
if any( (temp_1 - temp_2) <0 ) == 1 
    error('Check DATES in INTO') 
end 
clear temp junk; 
% ===========================================================
% Display 
% ============================================================
disp(' ');
disp('Loaded Dataset');
disp('=====================================');
disp('Series Names:');
printcell( yname' );
disp(' ');
disp('_______________');
disp('Sample settings');
disp(' ');
dispaj('Available  Sample: ',cvec(1),' TO ',cvec(end));
dispaj('Extracted  Sample: ',cvec(staf),' TO ',cvec(endf));
dispaj('Estimation Sample: ',cvec(estb),' TO ',cvec(este));
disp(' ');

% Extract Moments for the estimation sample 
% =========================================
[meany,stdy,flag_nan,numnan]=moments_nan(y(staf:endf,:),1,sernam(2:end));

if into.demean == 0
    flag_demean = 0 ;
    disp(' Warning....You are not demeaning the data') ;
    %meany=mean(y); 
else
    flag_demean = 1 ;
    disp('Data will be demeaned');
    if demean_type == 1
        dispaj('Mean for whole extracted sample');
        %meany=mean( y(staf:endf,:) );
        mean_demean=meanyf; 
    else
        dispaj('Mean for estimation sample only');
        %meany= mean( y(estb:este,:) );
        mean_demean=meany; 
    end
end
y=y(staf:endf,:);
cvec=cvec(staf:endf); 
estb = find(  (cvec  ==  into.startest ) );
este = find(  (cvec  ==  into.finishest) );
trainvec = [estb,este]; 
dispaj('Start estimation in observation: ',estb); 
if flag_demean == 1  
    T = size(y,1);     
    cc=repmat(mean_demean,[T 1] ); 
    y=y-cc;
    [meany,stdy,flag_nan,numnan]=moments_nan(y(staf:endf,:),1,sernam(2:end));    
end 
disp(' '); 
dispaj('T in Extracted  sample: ',T); 
dispaj('T in Estimation sample: ',size( y(estb:este,:), 1 ) );
if onscreen==1;quer('c'); end 
if T < 20 
    button=questdlg('Less than 20 observations available.Do you wish to continue? (Y/N) ',...
        'Warning! small sample',...
        'No','Yes','No'); 
    switch button 
        case 'No' 
            error('End Run');  
    end
end 
% =====================================
% Display means and Moments 
% ====================================
disp('Moments'); 
printcell( crtcell([meanyf(:) meany(:)],yname(:),{'Mean Full','Mean Estimation'}) ); 
printcell( crtcell([stdyf(:)  stdy(:)],yname(:),{'Std  Full','Std  Estimation'}) ); 
if onscreen==1; quer('c'); end; 
disp('End Loading Dataset'); 